package com.util;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;

import com.service.CookieService;
public abstract class HTTPUtils {
	
	private static final RequestConfig config = RequestConfig.custom()
			.setConnectTimeout(30000).setSocketTimeout(30000).build();
	
	public static String getCharset(String contentType){
		if(contentType == null || contentType.length() == 0){
			return "utf-8";
		}
		String []types = contentType.split(";");
		for(String type:types){
			String charset[] = type.split("=");
			if(charset.length == 2 && charset[0].contains("charset")){
				return charset[1].trim();
			}
		}
		return null;
	}
	
	
    public static String getRawHtml(String url) {
        //获取响应文件，即html，采用get方法获取响应数据
    	CookieStore cookieStore = new BasicCookieStore();
    	CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).setDefaultCookieStore(cookieStore).build();
    	 HttpGet httpget = new HttpGet(url);
    	 CloseableHttpResponse response = null;
    	 String resultString = null;
    	 GZIPInputStream gzin = null;
    	 for(int i = 0; i < 2;i++){
    		 try{
    			 response = httpclient.execute(httpget);
    			 int state = response.getStatusLine().getStatusCode();
    			 if(state == HttpStatus.SC_OK){
    				 HttpEntity entity = response.getEntity();
    				 Header header = entity.getContentEncoding();
    				 Header h = entity.getContentType();
//    			 System.out.println("--h  charset:"+getCharset(h!=null?h.getValue():null));
    				 if(header != null && header.toString().contains("gzip")){
    					 System.out.println("  "+header.getName()+"   "+header.getValue());
    					 InputStream inputStream = entity.getContent();
    					 gzin = new GZIPInputStream(inputStream);  
    					 InputStreamReader isr = new InputStreamReader(gzin,"utf-8");   
    					 BufferedReader br = new BufferedReader(isr);  
    					 StringBuffer sb = new StringBuffer();  
    					 String tempbf;  
    					 while ((tempbf = br.readLine()) != null) {  
    						 sb.append(tempbf);  
    						 sb.append("\r\n");  
    					 }  
    					 isr.close();  
    					 gzin.close(); 
    					 resultString = sb.toString();
    				 }else{
    					 resultString = EntityUtils.toString(response.getEntity(),"utf-8");
    				 }
    			 }else{
    				 resultString = EntityUtils.toString(response.getEntity(),"utf-8");
    			 }
    		 }catch(Exception e){
//    		 e.printStackTrace(); //多线程会有很多readtime out
    		 }
    		 if(!resultString.contains("window.onload=setTimeout")){
    			 break;
    		 }else{
    			 //设置该死的cookie
    			 Map<String,String> map = new HashMap<String,String>();
    			 String cookieString = CookieService.parseCookie(resultString);
    			 String cookies[] = cookieString.split(";");
    			 for(String str:cookies){
    				 String keyvalue[] = str.split("=");
    				 if(keyvalue.length == 2){
    					 map.put(keyvalue[0].trim(), keyvalue[1].trim());
    				 }
    			 }
    			 if(map.size() > 0){
    				 BasicClientCookie cookie = new BasicClientCookie("_ydclearance",map.get("_ydclearance"));
    				 String expires = map.get("expires");
    				 Date date = new Date(expires);
    				 cookie.setExpiryDate(date);
    				 cookie.setPath(map.get("path"));
    				 cookie.setDomain(map.get("domain"));
    				 cookieStore.addCookie(cookie);
    			 }
    		 }
    	 }
        return resultString;
    }

}